/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fl.location.ui;

import fl.location.core.MobileLocation;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.TiledLayer;

/**
 *
 * @author Zeor
 */
public class Map implements Frame, CommandListener {

    int[] tiles = new int[225];
    Image staticMap;
    Image staticMap2;
    TiledLayer map;
    LayerManager manager;
    MobileLocation mLoc;
    private int xOffSet = 4;
    private int yOffSet = 4;
    private int lastKey;

    public Map(MobileLocation mLoc) {
        for (int i = 0; i < 225; i++) {
            tiles[i] = i + 1;
        }
        this.mLoc = mLoc;
        getImage();
        manager = new LayerManager();
        updateMap();
        manager.append(map);

    }

    public void getImage(){
        try{
            String url = "http://maps.google.com/staticmap?maptype=roadmap?center=55.681308,12.576325&zoom=14&size=600x600&markers=55.681308,12.576325,redg&markers=55.671308,12.576325,greeng&key=ABQIAAAAq-Qz8TGIOsr6uFju08iHzxT5lTfaIV3_uH6bju3oJBbGgtb9qxTntKMOIi3VeneWnsfCit-ZallR3A";
            HttpConnection httpCon = null;
            InputStream is = null;
            /// Opening Http Connection
            httpCon = (HttpConnection) Connector.open(url,Connector.READ,false);
            int byteLength = (int)httpCon.getLength();
            is = httpCon.openInputStream();
            staticMap = Image.createImage(is);
//            if(staticMap2 != null){
//                System.out.println("Win");
//            } else{
//                System.out.println("Null");
//            }
            
//            int i = 0;
//            while(i != -1){
//                i = is.read();
//            }
        } catch(Exception ioex){
            
        }
    }
    
    public void updateMap() {
        try {
            // load the image
            //http://maps.google.com/staticmap?maptype=roadmap?center=55.681308,12.576325&zoom=14&size=600x600&markers=55.681308,12.576325,redg&markers=55.671308,12.576325,greeng&key=ABQIAAAAq-Qz8TGIOsr6uFju08iHzxT5lTfaIV3_uH6bju3oJBbGgtb9qxTntKMOIi3VeneWnsfCit-ZallR3A

//            staticMap = Image.createImage("/staticmap.gif");//tiledlayer1.gif");//test.bmp");
            // create the tiledlayer background
            map = new TiledLayer(15, 15, staticMap, 40, 40);

            for (int rows = 0; rows < 8; rows++) {
                for (int columns = 0; columns < 6; columns++) {
                    map.setCell(columns, rows, tiles[columns + xOffSet + ((yOffSet + rows) * 15)]);
                }
            }
            map.setPosition(0, 0);
        } catch (Exception ioex) {
            System.err.println(ioex.getMessage());
        }
    }

    public void draw(Graphics g) {
        //manager.paint(g, 0, 0);
        map.paint(g);
    }

    public void handleInput(int keyState) {

        if ((keyState & GameCanvas.UP_PRESSED) != 0) {
            if ((lastKey & GameCanvas.UP_PRESSED) == 0) {
                if (yOffSet > 0) {
                    yOffSet--;
                    updateMap();
                }
            }
        } else if ((keyState & GameCanvas.DOWN_PRESSED) != 0) {
            if ((lastKey & GameCanvas.DOWN_PRESSED) == 0) {
                if (yOffSet < 7) {
                    yOffSet++;
                    updateMap();
                }
            }
        } else if ((keyState & GameCanvas.LEFT_PRESSED) != 0) {
            if ((lastKey & GameCanvas.LEFT_PRESSED) == 0) {
                if (xOffSet > 0) {
                    xOffSet--;
                    updateMap();
                }
            }
        } else if ((keyState & GameCanvas.RIGHT_PRESSED) != 0) {
            if ((lastKey & GameCanvas.RIGHT_PRESSED) == 0) {
                if (xOffSet < 9) {
                    xOffSet++;
                    updateMap();
                }
            }
        }
        lastKey = keyState;
    }

    public void commandAction(Command arg0, Displayable arg1) {
    }
}
